<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="68px"
    >
      <el-button type="primary" @click="handleUpload"
      >点击上传文件<i class="el-icon-upload el-icon--right"></i>
      </el-button>
      <el-form-item label="文件名" prop="docName">
        <el-input
          v-model="queryParams.fileName"
          placeholder="请输入文件名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索
        </el-button>
        <el-button
          type="primary"
          icon="el-icon-refresh"
          size="mini"
          @click="refreshQuery"
        >刷新
        </el-button>
      </el-form-item>
    </el-form>
    <el-row>
      <el-col :span="6" style="padding-right:30px">
      <div class="grid-content quotatree"
            style="border: 1px solid #dfe4ed; height: 550px">
      <themeTree @themeNode="themeNode" :showcheckbox="false"/>
      </div>
        
      </el-col>
      <el-col :span="17">
        <el-table
          :data="tableData"
          border
          >
          <el-table-column
          width="150"
            align="center"
            prop="name"
            label="报表名称">
          </el-table-column>
          <el-table-column
          width="150"
            align="center"
            prop="uname"
            label="报表简称">
          </el-table-column>
          <!-- <el-table-column
            align="center"
            prop="lable"
            label="报表标签">
          </el-table-column>
          <el-table-column
            align="center"
            prop="description"
            label="报表描述">
          </el-table-column> -->
          <el-table-column
            align="center"
            prop="createBy"
            label="创建人"
           >
          </el-table-column>
          <el-table-column
            width="140"
            align="center"
            prop="createDate"
            label="创建时间">
          </el-table-column>
          <el-table-column align="center" width="120" label="操作">
            <template slot-scope="scope">
              <el-button type="danger" icon="el-icon-delete" circle @click="handleDelete(scope.row.id)"></el-button>
              <el-button type="primary" icon="el-icon-view" @click="previewExcel(scope.row.url)" circle></el-button>
            </template>
          </el-table-column>
          <el-table-column label="描述" type="expand">
      <template slot-scope="props">
        <el-form label-position="left" inline class="demo-table-expand">
          <el-form-item label="报表名称">
            <span>{{ props.row.name }}</span>
          </el-form-item>
          <el-form-item label="报表简称">
            <span>{{ props.row.uname }}</span>
          </el-form-item>
          <el-form-item label="报表标签">
            <span>{{ props.row.lable}}</span>
          </el-form-item>
          <el-form-item label="报表描述">
            <span>{{ props.row.description }}</span>
          </el-form-item>
          <el-form-item label="创建人">
            <span>{{ props.row.createBy }}</span>
          </el-form-item>
          <el-form-item label="创建时间">
            <span>{{ props.row.createDate }}</span>
          </el-form-item>
          <el-form-item label="报表链接">
            <span>{{ props.row.url }}</span>
          </el-form-item>
        </el-form>
      </template>
    </el-table-column>
        </el-table>
        <pagination
          v-show="totalCount > 0"
          :page-sizes="[5, 10, 20]"
          :page-size="5"
          :total="totalCount"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>


    <el-dialog
      title="报表文件上传"
      destroy-on-close
      v-dialogDrag
      :visible.sync="openUploadFileDialog"
      width="60%"
      center
    >
      <div class="app-container">
        <el-row>
          <el-col :span="12">
            <el-upload
              ref="upload"
              :limit=1
              drag
              accept=".xlsx,.xls,.txt,.csv"
              :action="upload.url"
              :name="upload.filePath"
              :headers="upload.headers"
              :multiple=false
              :data="innerForm"
              :auto-upload="false"
              :before-upload="beforeUpload"
              :on-exceed="onExceed"
              :on-success="uploadSuccess">
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
              <div class="el-upload__tip" style="color:red" slot="tip">提示:只能上传.xlsx,.xls,.csv,.txt文件</div>
            </el-upload>
          </el-col>
          <el-col :span="12">
            <el-form label-position="left" label-width="80px" :model="innerForm" ref="innerForm" :rules="innerRules">
              <el-form-item disabled prop="themeName" label="所属主题">
                <el-input v-model="innerForm.themeName"></el-input>
              </el-form-item>
              <el-form-item label="报表名称" prop="name">
                <el-input v-model="innerForm.name"></el-input>
              </el-form-item>
              <el-form-item label="报表简称" prop="uname">
                <el-input v-model="innerForm.uname"></el-input>
              </el-form-item>
              <el-form-item label="报表标签" prop="lable">
                <el-input v-model="innerForm.lable"></el-input>
              </el-form-item>
              <el-form-item label="报表描述">
                <el-input v-model="innerForm.description"></el-input>
              </el-form-item>
            </el-form>
            <div>
              <el-button type="primary" @click="submitFileForm">上 传 提 交</el-button>
              <el-button @click="cancelUpload">取 消</el-button>
            </div>
          </el-col>
        </el-row>
      </div>

    </el-dialog>
  </div>
</template>

<script>

import themeTree from '@/views/quota/reportUpload/themeTree.vue'
import {queryByFileName, queryByThemeId} from "@/api/quota/report/index.js"
import { deleteById, getDefineByTheme } from '@/api/quota/reportUpload/reportUpload'
import { getToken } from '@/utils/auth'

export default {
  name: 'reportUpload',
  components: { themeTree },
  data() {
    return {
      loading: false,
      openUploadFileDialog: false,
      tableData: [],
      totalCount: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: undefined,
        THEME_SET_ID: undefined,
        orderField: 'CREATE_DATE',
        order: 'desc',
        fileName: ''
      },
      upload: {
        filePath: 'filePath',
        headers: { Authorization: 'Bearer ' + getToken() },
        url: process.env.VUE_APP_BASE_API + '/quotaReport/upload',
        updateSupport: 0
      },
      innerForm: {
        name: undefined,
        uname: '',
        lable: '',
        description: '',
        themeSetId: undefined,
        themeName: ''
      },
      innerRules: {
        name: [
          { required: true, message: '报表名称不能为空', trigger: 'blur' }
        ],
        uname: [
          { required: true, message: '报表简称不能为空', trigger: 'blur' }
        ],
        lable: [
          { required: true, message: '报表标签不能为空', trigger: 'blur' }
        ],
        themeName: [
          { required: true, message: '所属主题不能为空', trigger: 'blur' }
        ]
      }
      // 表单参数
    }
  },
  created() {
    this.getList()
  },
  methods: {
    //预览excel文件
    previewExcel(val){
      this.$router.push({
        path:"/quota/report",
        query: {
          url:val
        }
      })
    },
    handleDelete(id) {
      console.log(id)
      this.$confirm('确定删除该选择项吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          deleteById(id).then((res) => {
            if (res.data) {
              this.getList()
              this.$message({
                message: '已删除',
                type: 'success'
              })
            }
          })
        })
        .catch(() => {
        })
    },
    onExceed(files, fileList) {
      this.$message({
        message: '已达到最大文件上传个数',
        type: 'warning'
      })
    },
    beforeUpload(file) {
      console.log(file)
    },
    handleUpload() {
      if (this.innerForm.themeSetId) {
        this.openUploadFileDialog = true
      } else {
        this.$message({
          message: '请先选择主题',
          type: 'warning'
        })
      }
    },
    themeNode(node) {
      this.innerForm.themeSetId = node.id
      this.innerForm.themeName = node.name
      this.queryParams.THEME_SET_ID = node.id
      queryByThemeId(this.queryParams).then((res)=>{
        console.log('themeres')
        console.log(res)
        this.tableData = res.data.list
        this.totalCount = res.data.totalCount
      }).catch((error)=>{
       this.$message("获取报表信息失败")
      })
    },
    submitFileForm() {
      this.$refs['innerForm'].validate((valid) => {
        if (valid) {
          this.$refs.upload.submit()
        }
      })
    },
    cancelUpload() {
      this.openUploadFileDialog = false
      this.$refs.upload.clearFiles()
      this.$refs['innerForm'].resetFields()
      this.innerForm.themeSetId = undefined
    },
    //上传完成方法
    uploadSuccess(response, file, fileList) {
      if (response.code === 0) {
        this.msgSuccess('上传成功')
        this.cancelUpload()
        this.getList()
      }
    },
    /** 查询文件管理数据 */
    getList() {
      getDefineByTheme(this.queryParams).then((res) => {
        this.tableData = res.data.list
        this.totalCount = res.data.totalCount
        console.log('tableData')
        console.log(this.tableData)
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      if(!this.queryParams.fileName){
        this.$message("请输入文件名")
        return
      }
      queryByFileName(this.queryParams).then((res)=>{
        console.log('res')
        console.log(res)
         this.tableData = res.data.list
        this.totalCount = res.data.totalCount
      }).catch((error)=>{
         this.$message("获取报表信息失败")
      })
    },
    refreshQuery() {
      this.queryParams.THEME_SET_ID = ''
      this.getList()
    }
  }
}
</script>
<style lang="less">
  .quotatree {
    overflow: auto;
  }
  .demo-table-expand .el-form-item{
    margin-right: 30px;
    margin-bottom: 0
  }
</style>
